package com.ycsj.goldmedalnewconcept.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.ycsj.goldmedalnewconcept.R;

import java.math.BigDecimal;

/**
 * 绘制弧形
 * Created by Administrator on 2018/12/26 0026.
 */

public class ArcView extends View {

    //控件默认的大小
    private static final int DEFAULT_MIN_WIDTH = 180;

    //定义默认圆弧的宽度
    private static final float DEFAULT_BORDER_WIDTH = 16f;

    //默认最小值和最大值文字距离上面圆环的距离
    private int padding = dipToPx(6);

    //外圆的画笔
    private Paint outCirclePaint;

    //进度弧形的画笔
    private Paint arcPaint;

    //内圆的画笔
    private Paint circlePaint;

    //绘制中间文字的画笔
    private Paint tvPaint;

    //绘制人数文字的画笔
    private Paint numPaint;

    //文字大小
    private float textSize = dipToPx(8);

    //默认的人数
    private int num = 10;

    //最小进度值
    private int min = 0;

    //最大进度值
    private int max = 20;

    public ArcView(Context context) {
        this(context, null);
    }

    public ArcView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }


    /**
     * 初始化画笔的操作
     */
    private void initPaint() {

        //初始化外圆的画笔
        outCirclePaint = new Paint();
        outCirclePaint.setAntiAlias(true);//扛锯齿
        outCirclePaint.setColor(getResources().getColor(R.color.out_circle_arc_color));
        outCirclePaint.setStyle(Paint.Style.FILL);

        //初始化内圆的画笔
        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);//扛锯齿
        circlePaint.setColor(getResources().getColor(R.color.circle_arc_color));
        circlePaint.setStyle(Paint.Style.FILL);

        //初始化进度圆环的画笔
        arcPaint = new Paint();
        arcPaint.setAntiAlias(true);//扛锯齿
        arcPaint.setColor(getResources().getColor(R.color.arcpbcolor));//画笔颜色
        arcPaint.setStyle(Paint.Style.STROKE);//填充样式
        arcPaint.setStrokeWidth(DEFAULT_BORDER_WIDTH);//画笔宽度

        //初始化中间文字的参与人数的画笔
        tvPaint = new Paint();
        tvPaint.setAntiAlias(true);//扛锯齿
        tvPaint.setColor(getResources().getColor(R.color.tv_arc_color));
        tvPaint.setTextAlign(Paint.Align.CENTER);
        tvPaint.setTextSize(textSize);

        //初始化人数值的画笔
        numPaint = new Paint();
        numPaint.setAntiAlias(true);//扛锯齿
        numPaint.setColor(getResources().getColor(R.color.tv_num_color));
        numPaint.setTextAlign(Paint.Align.CENTER);
//        numPaint.setTextSize(dipToPx(textSize + 1));
        numPaint.setTextSize(textSize + 16);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measure(widthMeasureSpec), measure(heightMeasureSpec));
    }

    /**
     * 计算控件宽高的方法
     */
    private int measure(int origin) {
        int result = DEFAULT_MIN_WIDTH;
        int specMode = MeasureSpec.getMode(origin);//获取模式
        int specSize = MeasureSpec.getSize(origin);//获取尺寸
        switch (specMode) {
            case MeasureSpec.EXACTLY:
                //EXACTLY是精确尺寸，当我们将控件的layout_width或layout_height指定为具体数值时如andorid:layout_width="50dip"，或者为FILL_PARENT是，都是控件大小已经确定的情况，都是精确尺寸。
                result = specSize;
                break;
            case MeasureSpec.AT_MOST:
                //AT_MOST是最大尺寸，当控件的layout_width或layout_height指定为WRAP_CONTENT时，控件大小一般随着控件的子空间或内容进行变化，此时控件尺寸只要不超过父控件允许的最大尺寸即可
                result = DEFAULT_MIN_WIDTH;
                break;
            case MeasureSpec.UNSPECIFIED:
                //UNSPECIFIED是未指定尺寸，这种情况不多，一般都是父控件是AdapterView，通过measure方法传入的模式。
                break;
            default:
                result = Math.min(result, specSize);
                if (result == 0) {
                    result = DEFAULT_MIN_WIDTH;
                }
                break;
        }
        return result;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //绘制外层圆
        canvas.drawCircle(getHeight() / 2, getHeight() / 2, getHeight() / 2, outCirclePaint);

        //绘制内层圆
        canvas.drawCircle(getHeight() / 2, getHeight() / 2, getHeight() / 2 - DEFAULT_BORDER_WIDTH, circlePaint);

        //绘制进度弧形
        RectF rect = new RectF(0 + DEFAULT_BORDER_WIDTH / 2, 0 + DEFAULT_BORDER_WIDTH / 2, getHeight() - DEFAULT_BORDER_WIDTH / 2, getHeight() - DEFAULT_BORDER_WIDTH / 2);
        //计算进度占用的角度
        float endAngle = 0;
        if (max == 0) {
            endAngle = 0;
        } else {
            endAngle = (float) (div(num, max, 2) * 360);
        }

        canvas.drawArc(rect, -270, endAngle, false, arcPaint);

        //绘制文字(参与人数)
        //计算出baseLine位置
        Paint.FontMetricsInt fm = tvPaint.getFontMetricsInt();
        //计算文字居中显示的基线
        int baseLineY = getHeight() / 2 + (fm.bottom - fm.top) / 2 - fm.bottom;
        canvas.drawText("参与人数", getWidth() / 2, baseLineY * 7 / 10, tvPaint);

        //绘制具体的数值
        Paint.FontMetricsInt fmNum = numPaint.getFontMetricsInt();
        int baseNumLineY = getHeight() / 2 + (fmNum.bottom - fmNum.top) / 2 - fmNum.bottom;
        canvas.drawText(num + "/" + max, getWidth() / 2, baseNumLineY * 12 / 10, numPaint);
    }


    /**
     * 设置数据的方法
     *
     * @param max 最大的人数值
     * @param num 参与人数的值
     */
    public void setArcViewData(int max, int num) {
        this.max = max;
        this.num = num;
        postInvalidate();
    }

    /**
     * dip 转换成px
     *
     * @param dip
     * @return
     */
    private int dipToPx(float dip) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * density + 0.5f * (dip >= 0 ? 1 : -1));
    }

    /**
     * 2个int相除 保留小数位 四舍五入
     *
     * @param v1
     * @param v2
     * @param scale
     * @return
     */
    public double div(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
